<template>
  <div class="login-container">
    <div class="login_top">
      <h1>健康管家机构后台</h1>
    </div>

    <!-- 登录 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h6 class="title">欢迎登录</h6>
      </div>

      <el-form-item prop="username" style="border: 1px solid #40b2aa;background:white;color:black">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="登录账户"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password" style="border: 1px solid #40b2aa;background:white">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="登录密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <div class="verifys">
        <el-input class="verify" v-model="input" placeholder="请输入验证码"></el-input>
        <img :src="imgUrl" @click="refreshImg" />
      </div>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;background:#40b2aa;border:none;"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
    <!-- <div class="login_tip">
      <p>商务服务，如有疑问请联系商务客服</p>
    </div>-->
  </div>
</template>
<script>
import request from "@/utils/request";
import { Message } from "element-ui";
import bgImg from "../../assets/login/bj.jpg";
export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码最少6位"));
      } else {
        callback();
      }
    };
    const validateUsername = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error("账户不能为空"));
      } else {
        callback();
      }
    };
    return {
      input: "",
      imgUrl: "",
      loginForm: {
        input: "",
        username: "",
        password: "",
        imgNum: 0
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {
    this.refreshImg();
  },
  methods: {
    //控制密码显隐藏
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    //登录
    handleLogin() {
      this.loginForm.input = this.input;
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(res => {
              this.loading = false;
              this.$router.push({ path: "/" });
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
      this.refreshImg();
      this.input = "";
    },
    //验证码
    refreshImg() {
      var num = Math.random();
      this.loginForm.imgNum = num;
      this.imgUrl =
       "https://www.zheliguan.com/v3/img/imgcode?math_code=" + num;
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
$light_gray: #fff;
$cursor: #454545;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: white;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #757575;
      height: 47px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .verifys {
    position: relative;
    span {
      position: absolute;
      top: 30px;
      margin-left: 15px;
      color: red;
    }
    img {
      width: 140px;
      height: 50px;
      margin-bottom: 10px;
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: white;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  height: 100%;
  position: relative;
  background-image: url("../../assets/login/bj.jpg");
  background-repeat: no-repeat;
  .login_top {
    width: 280px;
    color: white;
    position: absolute;
    top: 80px;
    left: 44%;
  }
  .login_tip {
    color: #717171;
    position: absolute;
    bottom: 200px;
    width: 300px;
    left: 43%;
  }
  .login-form {
    position: relative;
    width: 480px;
    height: 480px;
    max-width: 100%;
    padding: 0px 40px;
    margin: 0 auto;
    overflow: hidden;
    margin-top: 180px;
    background: white;
    border-radius: 20px;
    box-shadow: 3px 1px rgb(230, 230, 230), -2px -2px rgb(230, 230, 230);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .verify {
    width: 160px;
    background: white;
    overflow: hidden;
    border: 1px solid #40b2aa;
    margin-bottom: 20px;
    float: left;
    height: 50px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    margin-top: 60px;
    .title {
      font-size: 26px;
      color: #7a7a7a;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
